var current_active_ueitem = null;

UE.plugins["yead"] = function() {
    var me = this,
    editor = this;
    var utils = baidu.editor.utils,
    Popup = baidu.editor.ui.Popup,
    Stateful = baidu.editor.ui.Stateful,
    editorui = baidu.editor.ui,
    uiUtils = baidu.editor.ui.uiUtils,
    UIBase = baidu.editor.ui.UIBase;
    var domUtils = baidu.editor.dom.domUtils;

    //添加工具栏文字阴影图标
    var ci = 'shadowcolor';
    editorui[ci] = function(cmd) {
        return function(editor) {
            var ui = new editorui.ColorButton({
                className: 'edui-for-' + cmd,
                color: 'default',
                title: '文字阴影色',
                editor: editor,
                onpickcolor: function(t, color) {
                    editor.execCommand(cmd, color + " 2px 2px 10px");
                },
                onpicknocolor: function() {
                    editor.execCommand(cmd, 'default');
                    this.setColor('transparent');
                    this.color = 'default';
                },
                onbuttonclick: function() {
                    editor.execCommand(cmd, this.color + " 2px 2px 10px");
                }
            });
            editorui.buttons[cmd] = ui;
            editor.addListener('selectionchange',
            function() {
                ui.setDisabled(editor.queryCommandState(cmd) == -1);
            });
            return ui;
        };
    } (ci);
    
    var clickPop = new baidu.editor.ui.Popup({
        content: "",
        editor: me,
        _remove: function() {
            $(clickPop.anchorEl).remove();
            clickPop.hide();
        },
        _copy: function() {
            $(clickPop.anchorEl).prop('outerHTML');
            clickPop.hide();
        },
        _preblank: function() {
	        $('<p><br/></p>').insertBefore(clickPop.anchorEl);
            clickPop.hide();
        },
        _blank: function() {
            $('<p><br/></p>').insertAfter(clickPop.anchorEl);
            clickPop.hide();
        },
        className: 'edui-bubble'
    });

    me.addListener("click",
    function(t, evt) {
        evt = evt || window.event;
        var el = evt.target || evt.srcElement;
        if (el.tagName == "IMG") {
            return;
        }
        if ($(el).parents('.yead_editor').size() > 0) {
            el = $(el).parents('.yead_editor:first').get(0);

            //添加区域选中标识,用于替换颜色
            elother = $(el).parents().parent().find('.yead_editor');
            $(elother).removeClass('yead-selected');
            $(el).addClass('yead-selected');      

            current_active_ueitem = $(el);
            clickPop.render();
            var html = clickPop.formatHtml('<nobr class="content-operate"><span id="copy" stateful>复制</span><span id="cut" stateful>剪切</span><span onclick="$$._remove()" stateful>删除</span><span onclick="$$._preblank()" stateful>其前插入空行</span><span onclick="$$._blank()" stateful>其后插入空行</span></nobr>');
            var content = clickPop.getDom('content');            
            content.innerHTML = html;
            clickPop.anchorEl = el;
            clickPop.showAnchor(clickPop.anchorEl);

            var client = new ZeroClipboard($('#copy'));
            client.on('ready',function(event) {
                client.on('copy',function(event) {
                    event.clipboardData.setData('text/html', $(clickPop.anchorEl).prop('outerHTML'));
                    clickPop.hide();
                });
            });

            var cut_client = new ZeroClipboard($('#cut'));
            cut_client.on('ready',function(event) {
                cut_client.on('copy',function(event) {
                    event.clipboardData.setData('text/html', $(clickPop.anchorEl).prop('outerHTML'));
                    clickPop.hide();
                    $(clickPop.anchorEl).remove();
                });
            });
        } else {
            if (current_active_ueitem) {
                current_active_ueitem = null;
            }
        }
    });   
};